import {p} from "./ajax.js"
var a = window.location.search;
console.log(a);
var b = a.split("=");
console.log(b);
var goods_id = b[b.length-1];
console.log(goods_id);

var lunbotu = document.getElementById("iot-lunbotu");
var div2 = document.getElementById("iot-div2");
var img = document.createElement("img");
lunbotu.appendChild(img);
p("https://api-hmugo-web.itheima.net/api/public/v1/goods/detail?goods_id="+goods_id).then((res)=>{
                return res.message;
            }).then((message)=>{
                console.log(message);

var price = document.getElementById("iot-price");
price.style.marginTop = lunbotu.offsetHeight +30+ 'px';
price.innerHTML = "￥" + message.goods_price;
price.style.color = "red";
price.style.fontSize = "0.2rem"
             
var name = document.getElementById("iot-name");
name.innerHTML = message.goods_name;
name.style.width = "80%";

var introduce = document.getElementById("iot-goods-introduce");
introduce.innerHTML=message.goods_introduce;

// 轮播图
var i = 0;
var index = 0;
img.width = document.body.clientWidth;

for(var j = 0;j<message.pics.length;j++){
    var div = document.createElement("div");
    div2.appendChild(div);
    div.setAttribute("id","iot-div2-"+j);
}

img.src = message.pics[0].pics_sma;//初始化图片地址
document.getElementById("iot-div2-0").style.opacity = 0.8;//初始化小圆点

for(var j = 0;j<message.pics.length;j++){
    let jj = j;
    document.getElementById("iot-div2-"+j).addEventListener('click',change1);
    function change1(){
        img.src = message.pics[jj].pics_sma;
        document.getElementById("iot-div2-"+jj).style.opacity = 0.8;
        for(var k = 0;k<message.pics.length;k++){
            if(k!=jj){
                document.getElementById("iot-div2-"+k).style.opacity = 0.2;
            }
        }
        i = index = jj;
    }
}
function lunbo() {
    i++;
    if(i!=index){document.getElementById("iot-div2-"+index).style.opacity = 0.2}
    index++;
    if(index==message.pics.length){index=0};
    document.getElementById("iot-div2-"+index).style.opacity=0.8;
    if(i==message.pics.length){i=0};
    img.src = message.pics[i].pics_sma;
}
setInterval(lunbo,2000)//定时调用函数
})

            
    img.width = document.body.clientWidth;;
//窗口变化宽度设置
let getwinwidth = function(){
   img.width = img.width = document.body.clientWidth;;

}
window.addEventListener('resize',getwinwidth);//监听窗口大小变化